package com.example.mapper;

import com.example.pojo.Competition;
import com.example.pojo.CompetitionCategory;
import com.example.pojo.CompetitionParticipant;
import com.example.pojo.Submission;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface CompetitionMapper {

    @Insert("insert into competition_category(category_id,name,description) values(#{categoryId},#{name},#{description})")
    void addCategory(CompetitionCategory competitionCategory);

    void add(Competition competition);

    List<Competition> list();

    @Delete("delete from competition where competition_id = #{id}")
    void delete(Integer id);

    void update(Competition competition);

    @Select("select * from competition where competition_id = #{id}")
    Competition detail(Integer id);
//    查询用户参加了哪些比赛
    @Select("select * from competition where competition_id in (select competition_id from competition_participant where user_id = #{userId}) ")
    List<Competition> joined(Integer userId);

    @Delete("delete from competition_participant where competition_id = #{id} and user_id = #{userId}")
    void cancel(Integer id, Integer userId);

    @Insert("insert into competition_participant(competition_id,user_id,join_time) values(#{competitionId},#{userId},#{joinTime})")
    void join(CompetitionParticipant competitionParticipant);

    @Insert("insert into submission(competition_id,participant_id,article_id,submit_time,status,score) values(#{competitionId},#{participantId},#{articleId},#{submitTime},#{status},#{score})")
    void submit(Submission submission);

    @Select("select * from competition where organizer_id = #{userId} and competition_id = #{competitionId}")
    Competition findByUserId(Integer userId, Integer competitionId);

    @Select("select * from competition where competition_id in (select competition_id from submission where submission_id = #{submissionId} )")
    Competition findBySubmissionId(Long submissionId);

    @Update("update submission set score = #{score} where submission_id = #{submissionId}")
    void score(Long submissionId, Integer score);

    @Select("select * from competition_category")
    List<CompetitionCategory> listCategory();

    @Delete("delete from competition_category where category_id = #{id}")
    void deleteCategory(Integer id);

    @Update("update competition_category set name = #{name},description = #{description} where category_id = #{categoryId}")
    void updateCategory(CompetitionCategory competitionCategory);
}
